﻿
''' <summary>
''' グループ管理
''' </summary>
''' <remarks></remarks>
Public Class Groups
	Inherits UI.CommonPage

#Region " Declare "

	''' <summary>
	''' グループテーブル列情報
	''' </summary>
	''' <remarks></remarks>
	Private Enum _groupCol As Integer
		Actions
		Name
	End Enum

	''' <summary>DAO</summary>
	Protected dao As Db.IDaoGroup

#End Region

#Region " Handles "

	''' <summary>
	''' フォームロード
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		_dataBindGroups()
	End Sub

	''' <summary>
	''' テーブル行作成
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvGroups_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles grdvGroups.RowCreated
		Select Case e.Row.RowType
			Case DataControlRowType.Header
				e.Row.TableSection = TableRowSection.TableHeader
			Case DataControlRowType.DataRow
				e.Row.TableSection = TableRowSection.TableBody
			Case DataControlRowType.Footer
				e.Row.TableSection = TableRowSection.TableFooter
		End Select
	End Sub

	''' <summary>
	''' テーブル行バインド
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvGroups_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdvGroups.RowDataBound
		' 現在行がデータ行以外は処理無し
		If e.Row.RowType <> DataControlRowType.DataRow Then
			Return
		End If
	End Sub

	''' <summary>
	''' テーブル行コマンド
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub grdvGroups_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles grdvGroups.RowCommand
		Select Case e.CommandName
			Case "AddNew"
				_validAdd(e)
				_grdvGroupsCommandAddNew(e)
		End Select
	End Sub

	''' <summary>
	''' テーブル行編集
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Protected Sub grdvGroups_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles grdvGroups.RowEditing
		Me.grdvGroups.EditIndex = e.NewEditIndex
		_dataBindGroups()
	End Sub

	''' <summary>
	''' テーブル行削除
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Protected Sub grdvGroups_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles grdvGroups.RowDeleting
		_grdvGroupsCommandDelete(e)
	End Sub

	''' <summary>
	''' テーブル更新
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Protected Sub grdvGroups_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles grdvGroups.RowUpdating
		_validRowUpdating(e)
		If Not Me.IsValid Then
			Return
		End If

		_grdvGroupsCommandModify(e)
	End Sub

	''' <summary>
	''' テーブル編集キャンセル
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Protected Sub grdvGroups_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles grdvGroups.RowCancelingEdit
		Me.grdvGroups.EditIndex = -1
		_dataBindGroups()
	End Sub

#End Region

#Region " Methods "

	''' <summary>
	''' テーブルとデータのバインド
	''' </summary>
	''' <returns></returns>
	''' <remarks></remarks>
	Private Function _dataBindGroups() As Integer
		Dim lst As IList(Of GroupEntity)

		lst = dao.GetAll()

		Me.grdvGroups.DataSource = lst
		Me.grdvGroups.DataBind()
	End Function

	''' <summary>
	''' グループ追加
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _grdvGroupsCommandAddNew(ByVal e As GridViewCommandEventArgs)
		If Not Me.IsValid Then
			Return
		End If

		Dim val As GroupEntity
		val = New GroupEntity
		val.Name = DirectCast(Me.grdvGroups.FooterRow.Cells(0).FindControl("txtGroupAdd"), TextBox).Text

		dao.Add(val)

		' データ再取得
		Me.grdvGroups.EditIndex = -1
		_dataBindGroups()
	End Sub

	''' <summary>
	''' グループ変更
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _grdvGroupsCommandModify(ByVal e As GridViewUpdateEventArgs)

		Dim id As Integer
		id = Me.grdvGroups.DataKeys(e.RowIndex).Value

		' 変更キー値設定
		Dim val As GroupEntity
		val = New GroupEntity
		val.Id = id
		val.Name = _getTextBoxInGroupGrdvRow(e.RowIndex, _groupCol.Name, "txtGroup").Text

		' 変更
		dao.Upd(val)

		' データ再取得
		Me.grdvGroups.EditIndex = -1
		_dataBindGroups()
	End Sub

	''' <summary>
	''' グループ削除
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _grdvGroupsCommandDelete(ByVal e As GridViewDeleteEventArgs)
		Dim id As Integer
		id = Me.grdvGroups.DataKeys(e.RowIndex).Value

		' 変更キー値設定
		Dim val As GroupEntity
		val = New GroupEntity
		val.Id = id

		' 変更
		dao.Del(val)

		' データ再取得
		Me.grdvGroups.EditIndex = -1
		_dataBindGroups()
	End Sub

	''' <summary>
	''' 追加時の検証
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _validAdd(ByVal e As GridViewCommandEventArgs)
		Dim txt As TextBox
		Dim name As String
		Dim rc As Moca.Util.ValidateTypes

		Me.validGroup.IsValid = False

		txt = DirectCast(Me.grdvGroups.FooterRow.Cells(0).FindControl("txtGroupAdd"), TextBox)
		name = txt.Text

		rc = validator.Verify(name, Moca.Util.ValidateTypes.Required)
		If Not validator.IsValidRequired(rc) Then
			Me.setErrorMessage("グループ名", validGroup, New WebControl() {txt}, name, rc)
			Return
		End If

		Me.validGroup.IsValid = True
	End Sub

	''' <summary>
	''' 変更時の検証
	''' </summary>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub _validRowUpdating(ByVal e As GridViewUpdateEventArgs)
		Dim txt As TextBox
		Dim name As String
		Dim rc As Moca.Util.ValidateTypes

		Me.validGroup.IsValid = False

		txt = _getTextBoxInGroupGrdvRow(e.RowIndex, _groupCol.Name, "txtGroup")
		name = txt.Text

		rc = validator.Verify(name, Moca.Util.ValidateTypes.Required)
		If Not validator.IsValidRequired(rc) Then
			Me.setErrorMessage("グループ名", validGroup, New WebControl() {txt}, name, rc)
			Return
		End If

		Me.validGroup.IsValid = True
	End Sub

	''' <summary>
	''' グリッド上のテキストボックスを返す
	''' </summary>
	''' <param name="rowNo">行</param>
	''' <returns></returns>
	''' <remarks></remarks>
	Private Function _getTextBoxInGroupGrdvRow(ByVal rowNo As Integer, ByVal col As _groupCol, ByVal controlName As String) As TextBox
		Return CType(Me.grdvGroups.Rows(rowNo).Cells(col).FindControl(controlName), TextBox)
	End Function

#End Region

End Class
